package ch.manage.movie.informationFetcher;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import ch.manage.movie.business.Movie;

public abstract class AFetchMoviesFromIMDB {
	
	public abstract List<Movie> getMovies(List<Movie> unfetchedMovies);
	
	protected void downloadImage(String fromURL, String toPath) {
		InputStream is = null;
		OutputStream os = null;
		try {
			File f = new File(toPath);
			HttpURLConnection connection = (HttpURLConnection) new URL(fromURL)
					.openConnection();
			connection.setRequestMethod("GET");
			connection.setDoInput(true);
			connection.setDoOutput(true);
			connection.setUseCaches(false);
			connection.setConnectTimeout(1000);
			connection.addRequestProperty("Accept", "*/*");
			connection.addRequestProperty("Accept-Language", "de-de;q=0.5");
			connection.addRequestProperty("Accept-Encoding", "gzip, deflate");
			connection
					.addRequestProperty(
							"User-Agent",
							"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 2.0.50727; MS-RTC LM 8)");
			connection.connect();
			is = connection.getInputStream();
			os = new FileOutputStream(f);
			byte[] buffer = new byte[1024];
			int byteReaded = is.read(buffer);
			while (byteReaded != -1) {
				os.write(buffer, 0, byteReaded);
				byteReaded = is.read(buffer);
			}
			os.close();
		} catch (Exception e) {
		} finally {
			if (is != null)
				try {
					is.close();
				} catch (Exception e) {
				}
			if (os != null)
				try {
					os.close();
				} catch (Exception e) {
				}
		}
	}

}
